from jira import JIRA

url = "http://10.25.25.117:8080"
user = "195277"
password = "dingmiaobin"
projectName = "IPLATC"
mdPath = "C:\\nodeJSWorkspace\\xin3plat-control\\client\\MARKMAP.md";
levelSymbol = ["# ", "## ", "### ", "#### ", "##### ", "###### ", "- "];
finish="√"
unfinished="x"



if __name__ == '__main__':

    jira = JIRA(server=url, basic_auth=(user, password))
    # boardName="XCYBER 看板"
    # sprintName="xCyber2021-Sprint5"
    project = jira.project(projectName)
    print(project)
    # project.
    # issueList = jira.search_issues("project = {}".format(projectName))
    issueList = jira.search_issues("project = {} and issuetype = 故事".format(projectName),maxResults=50000)
    # issueList = jira.search_issues(
    #     "project = {} and issuetype in (故事,子任务) and summary ~ \"{}\"".format(projectName, "需求"))
    print(issueList)
    print(len(issueList))
    missionList = [];
    taskList = [];
    tree = [];
    for issue in issueList:
        issueType = issue.fields.issuetype.name
        issueSummary = issue.fields.summary;
        # issueSummary=""
        if issueType == "故事" and issueSummary.startswith("需求"):
            missionList.append(issue);
    # missionList.sort(key=lambda x:x.fields.summary);
    missionList.sort(key=lambda x: x.id);
    for issue in missionList:
        issueType = issue.fields.issuetype.name
        issueSummary = issue.fields.summary;
        issueUrl = url + "/browse/" + issue.key;
        issueDescription = issue.fields.description;

        issueSummarySplit = issueSummary.split(":");
        for nameIndex in range(len(issueSummarySplit) - 1):
            name = issueSummarySplit[nameIndex];
            if len(list(filter(lambda x: x[1] == name and x[0]==nameIndex, tree))) == 0:
                tree.append([nameIndex, name]);
        tree.append([len(issueSummarySplit) - 1, issueSummarySplit[-1],issueUrl,issueDescription]);
    print(tree)
    output="";
    for element in tree:
        if len(element)>2:
            flag=""
            if element[3]==None or len(element[3])==0:
                flag=unfinished;
            else:
                flag=finish;
            output = output + "\n" +"{}[{}]({})".format(levelSymbol[element[0]],element[1]+flag,element[2]);
        else:
            output=output+"\n"+levelSymbol[element[0]]+element[1];
    print(output)
    # for issue in issueList:
    #     issueType = issue.fields.issuetype.name
    #     issueSummary = issue.fields.summary;
    #     issueUrl = url + "/browser/" + issue.key;
    #     issueDescription = issue.fields.description;
    #     demand=None;
    #     if issueType == "故事":
    #         # demand = Demand()
    #         # issueSummary="";
    #         issueSummarySplit=issueSummary.split(":");
    #         tempName=None;
    #         for name in range(len(issueSummarySplit)-1):
    #             findResult = filter(lambda e :  e.name==name,tree);
    #             if findResult is not None and len(findResult)>0:
    #                 pass;
    #             else:
    #                 parentDemand=Demand(name,"分组");
    #                 if tempName is not None:
    #                     findResult = filter(lambda e: e.name == tempName, tree).__next__();
    #                     parentDemand.parent = findResult;
    #                 tempName = name;
    #
    #     elif issueType == "子任务":
    #         demand = Demand(issueSummary, issueType);
